import scrapy
import json
from ..items import MyItem


class ItemspiderSpider(scrapy.Spider):
    # 爬虫的名称，用于区分不同的爬虫，启动时需要指定
    name = "ItemSpider"
    # 允许爬取的域名列表，不在该列表的域名网站是会被Scrapy框架禁止的
    allowed_domains = ["game.gtimg.cn"]
    # 启动爬虫的地址列表
    start_urls = ["https://game.gtimg.cn/images/lol/act/img/js/items/items.js?ts=2883760"]

    # 对请求后得到的响应进行解析
    # 以Item作为目标，提取装备的数据，构建装备的Item
    def parse(self, response):
        item_json_obj = json.loads(response.text)
        cnt = 0
        for item in item_json_obj['items']:
            # 依次提取每件装备的信息
            itemId = item['itemId']
            name = item['name']
            price = item['price']
            maps = "|".join(item['maps'])
            keywords = item['keywords']
            item_desc = item['item_desc'].replace("\n", " ")
            cnt += 1
            # 构建装备的Item
            myItem = MyItem()
            myItem['itemId'] = itemId
            myItem['name'] = name
            myItem['price'] = price
            myItem['maps'] = maps
            myItem['keywords'] = keywords
            myItem['item_desc'] = item_desc
            # 通过yield将item发送到pipeline进行后续的处理
            yield myItem
